package com.moneywelcome.riskassembly.apiImpl;

import com.moneywelcome.api.ApplicationScorecardApi;
import com.moneywelcome.results.Result;
import com.moneywelcome.vo.ComputeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.InputStreamReader;


@RestController
@Slf4j
public class ApplicationScorecardApiImpl implements ApplicationScorecardApi {
    @Value("${pyPath}")
    private String pyPath;

    @Override
    public Result computeScore(ComputeVo vo){
//        String data=" 0.730526598 38 0 0 0";
        Process process = null;
        String result = "";
        StringBuffer terminalStr = new StringBuffer();
        terminalStr.append("python ").append(pyPath).append(" ")
                .append(vo.getAvailabilityRate()).append(" ")
                .append(vo.getAge()).append(" ")
                .append(vo.getOverdueOneMonth()).append(" ")
                .append(vo.getOverdueTwoMonth()).append(" ")
                .append(vo.getOverdueThreeMonth());
        try {
          process=Runtime.getRuntime().exec(terminalStr.toString());
            BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream(), "utf-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result = line;
            }
            in.close();
            process.waitFor();
            System.out.println(result);

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(result);
        } finally {
            if (null != process)
                process.destroy();
        }
        return Result.success(result);
    }
}
